const {
  isAny
} = require('bpmnlint-utils')/** * A rule that checks that no implicit split is modeled * starting from a task. * * users should model the parallel splitting gateway * explicitly instead. */
module.exports = function() {
  function check(node, reporter) {
    if (!isAny(node, [
      'bpmn:Task',
      'bpmn:Event'
    ])) {
      return
    }

    const outgoing = node.outgoing || []

    const outgoingWithoutCondition = outgoing.filter((flow) => {
      return !hasCondition(flow) && !isDefaultFlow(node, flow)
    })
    if (outgoingWithoutCondition.length > 1) {
      reporter.report(node.id, 'Flow splits implicitly')
    }
  }

  return {
    check
  }
}

// helpers /////////////////////////////

function hasCondition(flow) {
  return !!flow.conditionExpression
}

function isDefaultFlow(node, flow) {
  return node['default'] === flow
}
